package com.amos.knife4j.config;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.amos.knife4j.config.result.ResultEnums;
import com.amos.knife4j.config.result.ResultUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URL;

/**
 * 自定义拦截器实现
 *
 * @author wang_dgang
 * @since 2018-10-22 15:51:36
 */
public class WebInterceptor implements HandlerInterceptor {


    public WebInterceptor() {

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // 响应编码utf-8
        response.setCharacterEncoding("UTF-8");
        // 响应数据类型为json
        response.setContentType("application/json; charset=utf-8");
        // 响应失败处理
        boolean noError = errorHandler(response);
        if (!noError) {
            return false;
        }
        // 获取拦截地址
        String requestUrl = request.getServletPath();
        String contextPath = request.getContextPath();
        //System.out.println(request.getContextPath());//返回的是项目的根目录，项目放在Tomcat的ROOT下就为空，本地中如果没有配置Application context，也会返回空，反之配置了就返回配置的值
        //System.out.println(request.getRequestURI());//URI:Uniform Resource Identifier，资源定位符，我输出的这几个地址的地方。
        //System.out.println(request.getRequestURL());//URL:Uniform Resource Locator ,统一资源定位符，也就是资源定位的全路径，访问地址，比URI多了前面的HOST。
        //System.out.println(request.getServletPath());//返回的是去掉根目录之后的URI，https://blog.csdn.net/xiaokuikey/article/details/50517015，这里介绍的很详细。
        //System.out.println(request.getSession().getServletContext().getRealPath(request.getRequestURI()));//这是服务器的真实地址，如下图为本地服务器的路劲，涉及到文件上传到服务器等可用该路径。getRealPath(“可以在这里配后面的地址，比如文件的文件夹名”)
        //System.out.println(request.getSession().getServletContext().getRealPath("/"));
        //todo 补充校验逻辑
        return true;
    }


    /**
     * 响应错误处理
     *
     * @param response
     */
    private boolean errorHandler(HttpServletResponse response) throws Exception {
        // 响应状态码
        int statusCode = response.getStatus();
        // 响应失败直接拦截
        if (statusCode == 400) {
            response.getWriter().write(JSON.toJSONString(ResultUtil.error(ResultEnums.ERR_400)));
            return false;
        }
        if (statusCode == 401) {
            response.getWriter().write(JSON.toJSONString(ResultUtil.error(ResultEnums.ERR_401)));
            return false;
        }
        if (statusCode == 403) {
            response.getWriter().write(JSON.toJSONString(ResultUtil.error(ResultEnums.ERR_403)));
            return false;
        }
        if (statusCode == 404) {
            response.getWriter().write(JSON.toJSONString(ResultUtil.error(ResultEnums.ERR_404)));
            return false;
        }
        if (statusCode == 405) {
            response.getWriter().write(JSON.toJSONString(ResultUtil.error(ResultEnums.ERR_405)));
            return false;
        }
        if (statusCode == 500) {
            response.getWriter().write(JSON.toJSONString(ResultUtil.error(ResultEnums.ERR_500)));
            return false;
        }
        if (statusCode == 502) {
            response.getWriter().write(JSON.toJSONString(ResultUtil.error(ResultEnums.ERR_502)));
            return false;
        }
        if (statusCode == 503) {
            response.getWriter().write(JSON.toJSONString(ResultUtil.error(ResultEnums.ERR_503)));
            return false;
        }
        return true;
    }
}